﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Data.OleDb;

namespace MikroMagazaObject
{
    public partial class frmCariSend : XtraForm
    {
        public string CnnString { get; set; }
        public string PosgenelPath { get; set; }
        public string PoskonPath { get; set; }
        public string KasaSayisi { get; set; }

        public frmCariSend()
        {
            InitializeComponent();
        }

        private void CariExport(DateTime Tarih)
        {
            try
            {
                if (Directory.Exists(PosgenelPath) == true)
                {
                    OleDbConnection cn = new OleDbConnection(CnnString);
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.Connection = cn;
                    cmd.CommandText = "SELECT count(TBL_CARI.ID)  FROM TBL_CARI where LASTUPDATE >=@tarih";
                    cmd.Parameters.Add("@tarih", OleDbType.Date).Value = Tarih.RemoveTime();
                    cn.Open();

                    progressBarControl1.Properties.Minimum = 0;
                    progressBarControl1.Properties.Maximum = Cevir.Parse<int>(cmd.ExecuteScalar());


                    cmd.CommandText = "SELECT * FROM TBL_CARI where LASTUPDATE >=@tarih order by KOD";
                    cmd.CommandTimeout = 999999;

                    OleDbDataReader rd = cmd.ExecuteReader();
                    StreamWriter CariKod_File = new StreamWriter(PosgenelPath + "\\CARIKOD.IDX", false);
                    StreamWriter CariNo_File = new StreamWriter(PosgenelPath + "\\CARINO.IDX", false);
                    StreamWriter Cari_File = new StreamWriter(PosgenelPath + "\\CARI.DAT", false, System.Text.Encoding.GetEncoding("ibm857"));

                    string CariId = null;
                    string CariKod = null;
                    string CariUnvan = null;
                    string CariSatir = null;
                    string Adres = null;
                    string VDaire = null;
                    string Vno = null;
                    string Discount = null;
                    string Toplam_Satir = null;
                    string fiyatno = "1";

                    int sira = 1;

                    DataSet Sorted_dataset = new DataSet();
                    Sorted_dataset.Tables.Add("CARI");
                    Sorted_dataset.Tables["CARI"].Columns.Add("ID", typeof(System.String));
                    Sorted_dataset.Tables["CARI"].Columns.Add("SIRANO", typeof(System.String));

                    sira = 1;
                    while (rd.Read())
                    {
                        CariId = rd.GetInt32(0).ToString("000000");
                        CariKod = rd.GetString(1).Mid(0, 20);
                        fiyatno = rd.GetInt32(8).ToString("00");

                        if (CariKod.Length < 20)
                        {
                            CariKod = CariKod.Space(20 - CariKod.Length);
                        }

                        CariUnvan = rd.GetString(2);

                        if (rd[4] != DBNull.Value)
                        {
                            Adres = rd.GetString(6);
                        }
                        else
                        {
                            Adres = "";
                        }


                        CariSatir = CariUnvan + " " + Adres;

                        if (CariSatir.Length < 80)
                        {
                            CariSatir = CariSatir.Space(80 - CariSatir.Length);
                        }
                        else
                        {
                            CariSatir = CariSatir.Mid(0, 80);
                        }

                        VDaire = rd.GetString(4).Mid(0, 15);
                        if (VDaire.Length < 15)
                        {
                            VDaire = VDaire.Space(15 - VDaire.Length);
                        }

                        Vno = rd.GetString(5).Mid(0, 15);
                        if (Vno.Length < 15)
                        {
                            Vno = Vno.Space(15 - Vno.Length);
                        }

                        Discount = rd.GetInt32(7).ToString("00");


                        Sorted_dataset.Tables["CARI"].Rows.Add(CariKod, sira.ToString("000000"));
                        Toplam_Satir = "1" + CariId + CariKod + CariSatir + VDaire + Vno + Discount + fiyatno + new string(' ', 20);
                        Cari_File.WriteLine(Toplam_Satir);
                        CariNo_File.WriteLine(CariId + sira.ToString("000000"));

                        progressBarControl1.Position += 1;
                        sira = sira + 1;
                    }
                    Cari_File.Close();
                    CariNo_File.Close();

                    cn.Close();

                    DataView Sort_View = new DataView(Sorted_dataset.Tables["CARI"]);
                    Sort_View.Sort = "ID ASC";

                    for (int k = 0; k <= Sorted_dataset.Tables["CARI"].Rows.Count - 1; k++)
                    {
                        CariKod_File.WriteLine(Sort_View[k]["ID"].ToString() + Sort_View[k]["SIRANO"].ToString());
                    }

                    CariKod_File.Close();

                    foreach (string kasa in KasaSayisi.Split(','))
                    {
                        Mesaj_Yaz(1, false, int.Parse(kasa));
                    }
                    MessageBox.Show("Cariler Gönderildi !");
                }
                else
                {
                    MessageBox.Show(PosgenelPath + " Geçerli Bir Dosya Yolu Değil");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Mesaj_Yaz(int islem, bool tum, int kasa)
        {
            try
            {
                if (Directory.Exists(PoskonPath) == true)
                {
                    StreamWriter sr = new StreamWriter(PoskonPath + "\\MESAJ." + kasa.ToString("000"), false);
                    switch (islem)
                    {
                        case 0:
                            if (tum)
                            {
                                sr.WriteLine("107100000000000");
                            }
                            else
                            {
                                sr.WriteLine("1140000000000000001");
                            }
                            break;
                        case 1:
                            sr.WriteLine("108100000000000");
                            break;
                        case 2:
                            sr.WriteLine("11400000000100000000000000");
                            break;
                        case 3:
                            sr.WriteLine("11400000000000000000000010");
                            break;
                        case 4:
                            sr.WriteLine("119000000000000");
                            break;
                    }
                    sr.Close();
                }
                else
                {
                    MessageBox.Show(PoskonPath + " Geçerli bir Dosya yolu Değil");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DateTime tarih = new DateTime(1900, 1, 1);
            CariExport(tarih);
        }

        private void frmCariSend_Load(object sender, EventArgs e)
        {
        }
    }
}
